package com.oa.configuration;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.oa.common.mybatis.id.IdGenInterceptor;
import com.oa.common.mybatis.typehandler.Boolean2Number;
import com.oa.common.mybatis.typehandler.Set2String;
import org.apache.ibatis.type.TypeAliasRegistry;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author cuichao
 */
@Configuration
public class MyBatisExtensionConfiguration implements ConfigurationCustomizer {


    @Override
    public void customize(MybatisConfiguration configuration) {

        TypeAliasRegistry aliasRegistry = configuration.getTypeAliasRegistry();
        aliasRegistry.registerAlias("set2string", Set2String.class);
        aliasRegistry.registerAlias("boolean2number", Boolean2Number.class);

        TypeHandlerRegistry handlerRegistry = configuration.getTypeHandlerRegistry();
        handlerRegistry.register(Set2String.class);
        handlerRegistry.register(Boolean2Number.class);

        configuration.addInterceptor(new IdGenInterceptor());
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

}
